package huffmanTree;

import java.util.*;

/**
 * @program: data_structures_algorithms
 * @description: 霍夫曼树
 * @author: lld
 * @create: 2020-11-05 17:22
 **/
public class HuffmanTree {
    public static void main(String[] args) {
        int[] arr = {4, 13, 8, 6, 7};
        Node root = createHuffmanTree(arr);
        root.perOrder();//输出前序遍历
//        System.out.println(root);
//        Map<Integer, String> stringMap = getCode(root, "", new StringBuffer());
//        System.out.println(stringMap.keySet());
//        System.out.println(stringMap.get(13));
//        System.out.println(stringMap.get(8));

    }
///**
//* @Description: 哈夫曼编码
//* @Param:
//* @return:
//**/
//    static Map<Integer, String> map = new HashMap<>();
//
//    public static Map<Integer, String> getCode(Node root, String code, StringBuffer stringBuffer) {
//        StringBuffer stringBuffer1 = new StringBuffer(stringBuffer);
//        stringBuffer1.append(code);
//        if (root != null) {
//
//            if (root.left != null || root.right != null) {//非叶子结点
//                getCode(root.left, "0", stringBuffer1);
//                getCode(root.right, "1", stringBuffer1);
//            } else {
//                map.put(root.value, stringBuffer1.toString());
//            }
//        }
//        return map;
//    }

    /**
     * @Description: 构造霍夫曼树
     * @Param: [arr]
     * @return: huffmanTree.Node
     **/
    public static Node createHuffmanTree(int[] arr) {
        //将数组里的每一个元素构造成Node,并放入ArrayList中，便于排序
        List<Node> nodeList = new ArrayList<Node>();
        for (int i = 0; i < arr.length; i++) {
            nodeList.add(new Node(arr[i]));
        }
        while (nodeList.size() > 1) {
            //排序,从小到大排序取决于Node中重写的compareTo方法
            Collections.sort(nodeList);
            System.out.println("排序：" + nodeList);

            //取出权值最小的两个二叉树（节点）
            Node left = nodeList.get(0);
            Node right = nodeList.get(1);

            Node parent = new Node(left.value + right.value, left, right);
            //删除前两个
            nodeList.remove(left);
            nodeList.remove(right);

            nodeList.add(parent);
//            Collections.sort(nodeList);
//            System.out.println("排序2：" + nodeList);
        }
        return nodeList.get(0);
    }

}

/**
 * @Description: 使Node对象存储在集合中能够排序，要实现Comparable接口
 * @Param:
 * @return:
 **/
class Node implements Comparable<Node> {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    public Node(int value, Node left, Node right) {
        this.value = value;
        this.left = left;
        this.right = right;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    @Override
    public int compareTo(Node o) {
        //从小到大排序
        return this.value - o.value;
    }

    //输出前序遍历
    public void perOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.perOrder();
        }
        if (this.right != null) {
            this.right.perOrder();
        }
    }
}